package com.supply.service.impl;

import com.supply.mapper.PermissionMapper;
import com.supply.mapper.RolePermissionMapper;
import com.supply.pojo.Permission;
import com.supply.pojo.PermissionExample;
import com.supply.pojo.RolePermission;
import com.supply.pojo.RolePermissionExample;
import com.supply.req.PermissionReq;
import com.supply.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public List<Permission> queryByCondition(PermissionReq permissionReq) {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        if (!ObjectUtils.isEmpty(permissionReq.getName())) {
            criteria.andNameLike("%" + permissionReq.getName() + "%");
        }
        return permissionMapper.selectByExample(example);
    }

    @Override
    public int saveOrUpdate(Permission permission) {
        if (permission.getId() != null) {
            return permissionMapper.updateByPrimaryKey(permission);
        } else {
            return permissionMapper.insert(permission);
        }
    }

    @Override
    public int deleteById(Long id) {

        return permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Permission> queryAll() {
        return permissionMapper.selectByExample(null);
    }

    @Override
    public List<RolePermission> queryRoleAndPermissionByRoleId(Long id) {
        RolePermissionExample example = new RolePermissionExample();
        example.createCriteria().andRoleIdEqualTo(id);
        return rolePermissionMapper.selectByExample(example);
    }

    @Override
    public List<Permission> queryByRoleId(Long id) {
        List<Permission> permissions = new ArrayList<>();

        RolePermissionExample example = new RolePermissionExample();
        example.createCriteria().andRoleIdEqualTo(id);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectByExample(example);

//        List<Long> collect = rolePermissions.stream().map(e -> e.getPermission()).collect(Collectors.toList());
//        PermissionExample permissionExample = new PermissionExample();
//        permissionExample.createCriteria().andIdIn(collect);
//        List<Permission> permissionList = permissionMapper.selectByExample(permissionExample);
//        return permissionList;

        for (RolePermission rolePermission : rolePermissions) {
            Permission permission = permissionMapper.selectByPrimaryKey(rolePermission.getPermission());
            permissions.add(permission);
        }
        return permissions;
    }
}
